package com.itheima.dubbo.mongodb.impl;

import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.commons.model.mongo.Message;
import com.itheima.dubbo.interfaces.ManageApi;
import com.mongodb.client.result.UpdateResult;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: alongchan
 * @Date: 2021/12/19 14:57
 * @Description: 暂时未实现根据id, 时间查询
 */

@Service
public class ManageApiImpl implements ManageApi {

    private List<Message> messages = new ArrayList<>();

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询所有的message
    @Override
    public List<Message> getAll(Integer page, Integer pageSize, Long userId, String state, String sortProp, String sortOrder, Long startTime, Long endTime) {

        if (userId == null) {
            if (startTime == -1) {
                messages = mongoTemplate.find(Query.query(Criteria.where(""))
                        .with(Sort.by(sortProp)).with(PageRequest.of(page - 1, 12)), Message.class);
            } else {
                messages = mongoTemplate.find(Query.query(Criteria.where("created").gte(startTime).lte(endTime))
                        .with(Sort.by(sortProp)).with(PageRequest.of(page - 1, 12)), Message.class);
            }
        } else {
            if (startTime == -1) {
                messages = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId))
                        .with(Sort.by(sortProp)).with(PageRequest.of(page - 1, 12)), Message.class);
            } else {
                messages = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId).and("created").gte(startTime).lte(endTime))
                        .with(Sort.by(sortProp)).with(PageRequest.of(page - 1, 12)), Message.class);
            }
        }
        return messages;
    }

    //根据state状态查询message
    @Override
    public List<Message> getAllMessagesByState(Integer page, Integer pageSize, Long uid, String state, String sortProp, String sortOrder, Long startTime, Long endTime) {
        if (uid == null) {
            if (startTime == -1) {
                messages = mongoTemplate.find(Query.query(Criteria.where("state").is(Convert.toInt(state)))
                        .with(Sort.by(sortProp)).with(PageRequest.of(page - 1, 12)), Message.class);
            } else {
                messages = mongoTemplate.find(Query.query(Criteria.where("created").gte(startTime).lte(endTime).and("state").is(Convert.toInt(state)))
                        .with(Sort.by(sortProp)).with(PageRequest.of(page - 1, 12)), Message.class);
            }
        } else {
            if (startTime == -1) {
                messages = mongoTemplate.find(Query.query(Criteria.where("userId").is(uid).and("state").is(Convert.toInt(state)))
                        .with(Sort.by(sortProp)).with(PageRequest.of(page - 1, 12)), Message.class);
            } else {
                messages = mongoTemplate.find(Query.query(Criteria.where("userId").is(uid).and("state").is(Convert.toInt(state)).and("created").gte(startTime).lte(endTime))
                        .with(Sort.by(sortProp)).with(PageRequest.of(page - 1, 12)), Message.class);
            }
        }
        return messages;
    }

    //查询已通过的message(人审和机器审核)
    @Override
    public List<Message> getPassedMessagesByState(Integer page, Integer pageSize, Long userId, String state, String sortProp, String sortOrder, Long startTime, Long endTime) {
        if (userId == null) {
            if (startTime == -1) {
                messages = mongoTemplate.find(Query.query(Criteria.where("state").in(2,5))
                        .with(Sort.by(sortProp)).with(PageRequest.of(page - 1, 12)), Message.class);
            } else {
                messages = mongoTemplate.find(Query.query(Criteria.where("created").gte(startTime).lte(endTime).and("state").in(2,5))
                        .with(Sort.by(sortProp)).with(PageRequest.of(page - 1, 12)), Message.class);
            }
        } else {
            if (startTime == -1) {
                messages = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId).and("state").in(2,5))
                        .with(Sort.by(sortProp)).with(PageRequest.of(page - 1, 12)), Message.class);
            } else {
                messages = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId).and("state").in(2,5).and("created").gte(startTime).lte(endTime))
                        .with(Sort.by(sortProp)).with(PageRequest.of(page - 1, 12)), Message.class);
            }
        }
        return messages;
    }

    //查询Total（message中总数记录数）
    @Override
    public Integer findTotalCount(Long userId, Long startTime, Long endTime) {
        long count = 0L;
        if (userId == null) {
            if (startTime == -1) {
                count = mongoTemplate.count(Query.query(Criteria.where("")), "message");
            } else {
                count = mongoTemplate.count(Query.query(Criteria.where("").and("created").gte(startTime != -1 ? startTime : "created").lte(endTime != -1 ? endTime : "created")), "message");
            }
        } else {
            if (startTime == -1) {
                count = mongoTemplate.count(Query.query(Criteria.where("userId").is(userId)), "message");
            } else {
                count = mongoTemplate.count(Query.query(Criteria.where("userId").is(userId).and("created").gte(startTime != -1 ? startTime : "created").lte(endTime != -1 ? endTime : "created")), "message");
            }
        }
        return Convert.toInt(count);
    }

    //查询待审核的数量
    @Override
    public Integer findWaitCheckCount(Long userId, Long startTime, Long endTime) {
        long count = 0L;
        if (userId == null) {
            if (startTime == -1) {
                count = mongoTemplate.count(Query.query(Criteria.where("state").is(3)), "message");
            } else {
                count = mongoTemplate.count(Query.query(Criteria.where("state").is(3).and("created").gte(startTime != -1 ? startTime : "created").lte(endTime != -1 ? endTime : "created")), "message");
            }
        } else {
            if (startTime == -1) {
                count = mongoTemplate.count(Query.query(Criteria.where("userId").is(userId).and("state").is(3)), "message");
            } else {
                count = mongoTemplate.count(Query.query(Criteria.where("userId").is(userId).and("state").is(3).and("created").gte(startTime != -1 ? startTime : "created").lte(endTime != -1 ? endTime : "created")), "message");
            }
        }
        return Convert.toInt(count);
    }

    //查询通过审核的数量
    @Override
    public Integer findPassCount(Long userId, Long startTime, Long endTime) {
        long count = 0L;
        if (userId == null) {
            if (startTime == -1) {
                count = mongoTemplate.count(Query.query(Criteria.where("state").in(2, 5)), "message");
            } else {
                count = mongoTemplate.count(Query.query(Criteria.where("state").in(2, 5).and("created").gte(startTime != -1 ? startTime : "created").lte(endTime != -1 ? endTime : "created")), "message");
            }
        } else {
            if (startTime == -1) {
                count = mongoTemplate.count(Query.query(Criteria.where("userId").is(userId).and("state").in(2, 5)), "message");
            } else {
                count = mongoTemplate.count(Query.query(Criteria.where("userId").is(userId).and("state").in(2, 5).and("created").gte(startTime != -1 ? startTime : "created").lte(endTime != -1 ? endTime : "created")), "message");
            }
        }
        return Convert.toInt(count);
    }

    //查询拒绝的数量
    @Override
    public Integer findRejectCount(Long userId, Long startTime, Long endTime) {
        long count = 0L;
        if (userId == null) {
            if (startTime == -1) {
                count = mongoTemplate.count(Query.query(Criteria.where("state").is(4)), "message");
            } else {
                count = mongoTemplate.count(Query.query(Criteria.where("state").is(4).and("created").gte(startTime != -1 ? startTime : "created").lte(endTime != -1 ? endTime : "created")), "message");
            }
        } else {
            if (startTime == -1) {
                count = mongoTemplate.count(Query.query(Criteria.where("userId").is(userId).and("state").is(4)), "message");
            } else {
                count = mongoTemplate.count(Query.query(Criteria.where("userId").is(userId).and("state").is(4).and("created").gte(startTime != -1 ? startTime : "created").lte(endTime != -1 ? endTime : "created")), "message");
            }
        }
        return Convert.toInt(count);
    }

    //消息通过
    @Override
    public Boolean passMessage(String id) {
        UpdateResult updateResult = mongoTemplate.updateFirst(Query.query(Criteria.where("publishId").is(new ObjectId(id))), Update.update("state", 5), Message.class);
        if (updateResult != null) {
            return true;
        } else return false;
    }

    //消息拒绝
    @Override
    public Boolean rejectMessage(String id) {
        UpdateResult updateResult = mongoTemplate.updateFirst(Query.query(Criteria.where("publishId").is(new ObjectId(id))), Update.update("state", 4), Message.class);
        if (updateResult != null) {
            return true;
        } else return false;
    }

    //消息撤销
    @Override
    public Boolean revocationMessage(String id) {
        UpdateResult updateResult = mongoTemplate.updateFirst(Query.query(Criteria.where("publishId").is(new ObjectId(id))), Update.update("state", 3), Message.class);
        if (updateResult != null) {
            return true;
        } else return false;
    }

    //根据publishId查询状态
    @Override
    public String findMessageStateByPublishId(ObjectId id) {
        Message message = mongoTemplate.findOne(Query.query(Criteria.where("publishId").is(id)), Message.class);
        return message.getState();
    }

    //审核后的message存入message表
    @Override
    public void addMessage(Message message) {
        mongoTemplate.insert(message);
    }

    //查询message是否置顶
    @Override
    public Integer findMessageTopState(String publishId) {
        Message one = mongoTemplate.findOne(Query.query(Criteria.where("publishId").is(new ObjectId(publishId))), Message.class);
        return Convert.toInt(one.getTopState());
    }

    //修改message为置顶
    @Override
    public Boolean modifyMessageTop(ObjectId publishId) {
        UpdateResult updateResult = mongoTemplate.updateFirst(Query.query(Criteria.where("publishId").is(publishId)), Update.update("topState", 2), Message.class);
        if (updateResult != null) {
            return true;
        } else {
            return false;
        }
    }

    //取消置顶
    @Override
    public Boolean modifyMessageUntop(ObjectId publishId) {
        UpdateResult updateResult = mongoTemplate.updateFirst(Query.query(Criteria.where("publishId").is(publishId)), Update.update("topState", 1), Message.class);
        if (updateResult != null) {
            return true;
        } else {
            return false;
        }
    }

    //查询用户所有动态
    @Override
    public List<Message> findUserMovementsRecord(Integer page, Integer pageSize, Long userId) {
        return mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)).with(PageRequest.of(page - 1, pageSize)).with(Sort.by(Sort.Order.desc("topState"))), Message.class);
    }
}
